﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using JelloPhysics;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace WinFormDemo
{
    class MyPanel : Panel
    {
        public MyPanel()
        {
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.UserPaint, true);
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            //base.OnPaintBackground(e);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            //base.OnPaint(e);
            Bitmap bmp = new Bitmap(this.Width, this.Height);
            Graphics g = Graphics.FromImage(bmp);
            g.Clear(Color.White);
            //g.CompositingMode = CompositingMode.SourceCopy;
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.InterpolationMode = InterpolationMode.High;
            g.SmoothingMode = SmoothingMode.HighQuality;
            Form1 form = this.Parent as Form1;
            if (form != null)
            {
                List<Body> barBody = form.staticBodies;
                List<Polygon> ballList = form.ballList;
                OnPaint(g, barBody, ballList);
            }
            e.Graphics.DrawImage(bmp, 0, 0);
        }

        private void OnPaint(System.Drawing.Graphics graphics, List<Body> barBody, List<Polygon> ballList)
        {
            foreach (Body body in barBody)
            {
                Polygon poly = body.ObjectTag as Polygon;
                if (poly != null)
                {
                    List<CPoint> points = poly.Points;
                    if (points.Count == 4)
                    {
                        Pen p = new Pen(poly.Stroke, poly.StrokeThickness);
                        GraphicsPath path = new GraphicsPath();
                        Point[] pos = new Point[points.Count];
                        for (int n = 0; n < points.Count; n++)
                        {
                            pos[n] = new Point((int)points[n].X, (int)points[n].Y);
                        }
                        path.AddPolygon(pos);
                        graphics.DrawPath(p, path);
                        graphics.FillPath(poly.Fill, path);
                    }
                }
            }
            foreach (Polygon objectTag in ballList)
            {
                List<CPoint> points = objectTag.Points;
                if (points.Count > 4)
                {
                    Pen p = new Pen(objectTag.Stroke, objectTag.StrokeThickness);
                    GraphicsPath path = new GraphicsPath();
                    Point[] pos = new Point[points.Count];
                    for (int n = 0; n < points.Count; n++)
                    {
                        pos[n] = new Point((int)points[n].X, (int)points[n].Y);
                    }
                    path.AddPolygon(pos);
                    graphics.DrawPath(p, path);
                    graphics.FillPath(objectTag.Fill, path);
                }
            }
        }

    }
}
